package com.zhang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhang.common.dto.MenuDto;
import com.zhang.entity.SysMenu;
import com.zhang.entity.SysUser;
import com.zhang.mapper.SysMenuMapper;
import com.zhang.mapper.SysUserMapper;
import com.zhang.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张立松
 * @since 2022-12-22
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysMenuMapper sysMenuMapper;

    //获取用户导航栏
    @Override
    public List<MenuDto> getUserNav(SysUser user) {
        List<MenuDto> nav = new ArrayList<MenuDto>();

        List<Long> menuIds = sysUserMapper.getNavMenuIds(user.getId());
        List<SysMenu> menus = sysMenuMapper.selectBatchIds(menuIds);
        setMenuDtoTree(menus,nav);

        return nav;
    }

    //用用户的菜单构建树形导航结构
    private void setMenuDtoTree(List<SysMenu> menus, List<MenuDto> nav) {
        boolean hasParent = false;
        for(SysMenu m : menus){
            if(m.getType() == 1){
                Long parentId = m.getParentId();
                SysMenu parentMenu = sysMenuMapper.selectById(parentId);
                for(MenuDto n : nav){
                    if(n.getId() == parentId){  //如果此菜单的父级菜单已经存在则直接添加
                        hasParent = true;
                        n.getChildren().add(new MenuDto(m.getId(),m.getPerm(),m.getName(),m.getIcon(),m.getPath(),m.getComponent()));
                        break;
                    }
                    hasParent = false;
                }
                if(hasParent == false){ //如果此菜单的父级菜单不存在则添加父级菜单再添加自己
                    List<MenuDto> childrenInParent = new ArrayList<>();
                    childrenInParent.add(new MenuDto(m.getId(),m.getPerm(),m.getName(),m.getIcon(),m.getPath(),m.getComponent()));
                    nav.add(new MenuDto(parentMenu.getId(),parentMenu.getPerm(),parentMenu.getName(),parentMenu.getIcon(),parentMenu.getPath(),parentMenu.getComponent(),childrenInParent));
                }
            }
        }
    }

    //获取转成树状结构的菜单列表
    @Override
    public List<SysMenu> getMenuTreeList() {
        List<SysMenu> menuTree = new ArrayList<>();
        List<SysMenu> menus = sysMenuMapper.selectList(new QueryWrapper<SysMenu>().orderByAsc("orderNum"));
        setMenuTree(menus,menuTree);

        return menuTree;
    }

    //构建全部菜单的树形结构
    private void setMenuTree(List<SysMenu> menus, List<SysMenu> menuTree){
        menus.forEach(menu -> { //查找每个菜单的所有孩子，最后父id=0即为树的第一级菜单
            menus.forEach(m -> {
                if(m.getParentId() == menu.getId()){
                    menu.getChildren().add(m);
                }
            });
            if(menu.getParentId() == 0L){
                menuTree.add(menu);
            }
        });
    }
}
